//
//  NewFeatureViewController.swift
//  Tanenbaum_blog
//
//  Created by Tanenbaum on 15/7/31.
//  Copyright © 2015年 GZJ. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {
    
    
    private let imageCount = 4
    private let layout = ZuoJFlowout()
    
    //初始化方法
    init () {
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        //        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        // Do any additional setup after loading the view.
    }
    
    
    
    // MARK: - 数据源方法
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let path = collectionView.indexPathsForVisibleItems().last!
        
        if path.item == imageCount - 1 {
            
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            
            cell.prepareAnima()
        }
    }
    
    
}

//自定义cell的类
class NewFeatureCell: UICollectionViewCell {
    
    private var imageIndex: Int = 0 {
        
        didSet {
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    
    func Clicked() {
        
        NSNotificationCenter.defaultCenter().postNotificationName(ZuoJRootViewControllerSwitchNotification, object: true)
    }
    
    //按钮动画
    private func prepareAnima() {
        
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            
             self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
                self.startButton.userInteractionEnabled = true
        }
        
    }
    
    //初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    //设置视图UI，添加约束
    private func setupUI() {
        
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        
        //图片
        iconView.ff_Fill(contentView)
        //按钮约束
        startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
        
    }
    
    //MARK ： --懒加载控件
    //图片
    private lazy var iconView = UIImageView()
    //按钮
    private lazy var startButton: UIButton = {
        
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.sizeToFit()
        
        button.addTarget(self, action: "Clicked", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
}

private class ZuoJFlowout: UICollectionViewFlowLayout {
    
    private override func prepareLayout() {
        itemSize = collectionView!.bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
    
}

